/**
 * 
 */
package com.sip.comadapter.executor.common;

//import org.apache.log4j.Logger;

import com.sip.comadapter.common.AdaptorConstant;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.comserver.communication.utils.CommonUtils;


/**
 * @author Dhanushka Ariyasingha.
 *
 */
public class ExecutionManager {
	

	private CommunicationMessage message;
	
	public ExecutionManager(final CommunicationMessage message){
		this.message = message;
	}
	
    /**
     * Method will route and execute message.
     * 
     * @return
     * @throws Exception
     */
    public CommunicationMessage route() throws Exception {

        final String metadata = message.getMessageHeader();

        System.out.println("Message Header===>" + metadata);
        System.out.println("Message body=====>" + message.getMessageBody());

        final String executorName = CommonUtils.find(metadata, AdaptorConstant.HEDARE_TOKEN_EXECUTOR);

        final Executor executor = ExecutorLoadFactory.getInstance().getExecutor(executorName);
        CommunicationMessage outputMessage = null;

        try {

            outputMessage = executor.execute(message);

        } catch (final Exception exception) {

            exception.printStackTrace();
            CommunicationMessage returnMessage =new CommunicationMessage();
            returnMessage.setObject(exception);
            
            return returnMessage;
        }

        return outputMessage;
    }

}
